<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\Stocking;

use App\Jobs\Stocking\SkuApproval;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
class SkuBatchApproval extends BatchAction
{
    public $name = '通过';

    public function handle(Collection $collection)
    {
        try{
            $count = 0;
            $date = date('Y-m-d');
            $items = [];
            foreach ($collection as $model) {
                if($model->status != 2){
                    continue;
                }
                $model->status = 4;
                $model->approval_date = $date;
                $flag = $model->save();
                if($flag){
                    $count++;
                    $items[] = $model;
                }
            }
            if(!empty($items)){
                SkuApproval::dispatch($items);
            }
            return $this->response()->success("成功通过{$count}条记录！")->refresh();
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
